package com.zhipu.balance.utils.print.corn

import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.mobile.event.RecordCorn
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.print.BrightekCommandM
import java.nio.charset.Charset

object CornPrintUtils {

    /**
     * 玉米打印
     */
    fun CornPrinting(mRecordCorn : RecordCorn){
        val stringBuilder = StringBuilder()
        val title = "玉米计算"
        val retract = ((1.0 - title.length / 16.0) * 16).toInt()
        for (i in 0 until retract) {
            stringBuilder.append(" ")
        }
        stringBuilder.append(title).append('\r')
        //编号
        stringBuilder.append("编号：").append(mRecordCorn.uniqueId).append('\r')
        //时间
        stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordCorn.inspectionDate)).append('\r')
        //杂质记录员
        if (!TextUtils.isEmpty(mRecordCorn.impurityRecorder)) {
            stringBuilder.append("\r")
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("杂质").append('\r')
            stringBuilder.append("\r")
            // 大样试验-试样质量
            stringBuilder.append("大样质量(G)：").append(mRecordCorn.largeMiscellaneousSampleQuality).append('\r');
            //大样试验-杂质
            stringBuilder.append("大样杂质(G)：").append(mRecordCorn.detailImpurity).append('\r');
            //大样杂质率
            stringBuilder.append("大样杂质率(%)：").append(mRecordCorn.detailResults).append('\r');
            //小样试验-试样质量
            stringBuilder.append("小样质量(G)：").append(mRecordCorn.smallMiscellaneousSampleQuality).append('\r');
            //小样试验-杂质
            stringBuilder.append("小样杂质(G)：").append(mRecordCorn.smallSampleImpurity).append('\r');
            //小样杂质率
            stringBuilder.append("小样杂质率(%)：").append(mRecordCorn.sampleResults).append('\r');
            //杂质总量
            stringBuilder.append("杂质总量(%)：").append(mRecordCorn.impurityResults).append('\r');
            //杂质记录员
            stringBuilder.append("杂质记录员：").append(mRecordCorn.impurityRecorder).append('\r');
        }
        //不完善粒记录员
        if (!TextUtils.isEmpty(mRecordCorn.imperfectGrainRecorder)) {
            stringBuilder.append("\r")
            var i = 0
            while (i < (1.0 - 4 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("不完善粒").append('\r')
            stringBuilder.append("\r")
            if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){
                //虫蚀粒
                stringBuilder.append("虫蚀粒(G)：").append(mRecordCorn.insectBoredQuality).append('\r')
                //虫蚀率率
                stringBuilder.append("虫蚀率率(%)：").append(mRecordCorn.insectBoredResult).append('\r')
                //病斑粒
                stringBuilder.append("病斑粒(G)：").append(mRecordCorn.mottledQuality).append('\r')
                //病斑粒率
                stringBuilder.append("病斑粒率(%)：").append(mRecordCorn.mottledResult).append('\r')
                //破损粒
                stringBuilder.append("破损粒(G)：").append(mRecordCorn.brokenQuality).append('\r')
                //破损粒率
                stringBuilder.append("破损粒率(%)：").append(mRecordCorn.brokenResult).append('\r')
                //生芽粒
                stringBuilder.append("生芽粒(G)：").append(mRecordCorn.sproutWeight).append('\r')
                //生芽粒率
                stringBuilder.append("生芽粒率(%)：").append(mRecordCorn.sproutResult).append('\r')
                //生霉粒
                stringBuilder.append("生霉粒(G)：").append(mRecordCorn.moldWeight).append('\r')
                //生霉粒率
                stringBuilder.append("生霉粒率(%)：").append(mRecordCorn.moldResult).append('\r')
                //热损伤粒质量
                stringBuilder.append("热损伤粒质量(G)：").append(mRecordCorn.thermalDamageGrainQuality).append('\r')
                //热损伤粒结果
                stringBuilder.append("热损伤粒率(%)：").append(mRecordCorn.thermalDamageGrain).append('\r')
                //霉变粒质量
                stringBuilder.append("霉变粒质量(G)：").append(mRecordCorn.mildewGrainQuality).append('\r')
                //霉变粒结果
                stringBuilder.append("霉变粒率(%)：").append(mRecordCorn.mildewGrain).append('\r')
            }else{
                //不完善粒质量
                stringBuilder.append("不完善粒质量(G)：").append(mRecordCorn.imperfectGrainQuality).append('\r')
                //霉变粒质量
                stringBuilder.append("霉变粒质量(G)：").append(mRecordCorn.mildewGrainQuality).append('\r')
                //霉变粒结果
                stringBuilder.append("霉变粒率(%)：").append(mRecordCorn.mildewGrain).append('\r')
            }
            //不完善粒-结果
            stringBuilder.append("不完善粒率(%)：").append(mRecordCorn.resultOfImperfectGrain).append('\r')
            //不完善粒记录员
            stringBuilder.append("不完善粒记录员：").append(mRecordCorn.imperfectGrainRecorder).append('\r')
        }
        /**
         * 容重记录员
         */
        if (!TextUtils.isEmpty(mRecordCorn.densityRecorder)) {
            stringBuilder.append("\r")
            var i = 0
            while (i < (1.0 - 4 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("容重").append('\r')
            stringBuilder.append("\r")

            //容重-结果
            stringBuilder.append("容重-质量：").append(mRecordCorn.densityQuality).append('\r')
            //容重-结果
            stringBuilder.append("容重-结果：").append(mRecordCorn.densityResult).append('\r')
            //容重记录员
            stringBuilder.append("容重记录员：").append(mRecordCorn.densityRecorder).append('\r')
        }

        var i = 0
        while (i < (1.0 - 2 / 16.0) * 16) {
            stringBuilder.append(" ")
            i++
        }
        InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
        InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
    }


    /**
     * 玉米打印
     */
    fun CornPrinting(mRecordCornList: List<RecordCorn>,mSubclass: String){
        for (mRecordCorn in mRecordCornList){
            if(!mRecordCorn.select){
                continue
            }
            val stringBuilder = StringBuilder()
            val title = "玉米计算"
            val retract = ((1.0 - title.length / 16.0) * 16).toInt()
            for (i in 0 until retract) {
                stringBuilder.append(" ")
            }
            stringBuilder.append(title).append('\r')
            //编号
            stringBuilder.append("编号：").append(mRecordCorn.uniqueId).append('\r')
            //时间
            stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordCorn.inspectionDate)).append('\r')
            when (mSubclass) {
                "杂质" -> {
                    //杂质记录员
                    if (!TextUtils.isEmpty(mRecordCorn.impurityRecorder)) {
                        stringBuilder.append("\r")
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("杂质").append('\r')
                        stringBuilder.append("\r")
                        // 大样试验-试样质量
                        stringBuilder.append("大样质量(G)：").append(mRecordCorn.largeMiscellaneousSampleQuality).append('\r');
                        //大样试验-杂质
                        stringBuilder.append("大样杂质(G)：").append(mRecordCorn.detailImpurity).append('\r');
                        //大样杂质率
                        stringBuilder.append("大样杂质率(%)：").append(mRecordCorn.detailResults).append('\r');
                        //小样试验-试样质量
                        stringBuilder.append("小样质量(G)：").append(mRecordCorn.smallMiscellaneousSampleQuality).append('\r');
                        //小样试验-杂质
                        stringBuilder.append("小样杂质(G)：").append(mRecordCorn.smallSampleImpurity).append('\r');
                        //小样杂质率
                        stringBuilder.append("小样杂质率(%)：").append(mRecordCorn.sampleResults).append('\r');
                        //杂质总量
                        stringBuilder.append("杂质总量(%)：").append(mRecordCorn.impurityResults).append('\r');
                        //杂质记录员
                        stringBuilder.append("杂质记录员：").append(mRecordCorn.impurityRecorder).append('\r');
                    }
                }
                "不完善" -> {
                    //不完善粒记录员
                    if (!TextUtils.isEmpty(mRecordCorn.imperfectGrainRecorder)) {
                        stringBuilder.append("\r")
                        var i = 0
                        while (i < (1.0 - 4 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("不完善粒").append('\r')
                        stringBuilder.append("\r")
                        if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){
                            //虫蚀粒
                            stringBuilder.append("虫蚀粒(G)：").append(mRecordCorn.insectBoredQuality).append('\r')
                            //虫蚀率率
                            stringBuilder.append("虫蚀率率(%)：").append(mRecordCorn.insectBoredResult).append('\r')
                            //病斑粒
                            stringBuilder.append("病斑粒(G)：").append(mRecordCorn.mottledQuality).append('\r')
                            //病斑粒率
                            stringBuilder.append("病斑粒率(%)：").append(mRecordCorn.mottledResult).append('\r')
                            //破损粒
                            stringBuilder.append("破损粒(G)：").append(mRecordCorn.brokenQuality).append('\r')
                            //破损粒率
                            stringBuilder.append("破损粒率(%)：").append(mRecordCorn.brokenResult).append('\r')
                            //生芽粒
                            stringBuilder.append("生芽粒(G)：").append(mRecordCorn.sproutWeight).append('\r')
                            //生芽粒率
                            stringBuilder.append("生芽粒率(%)：").append(mRecordCorn.sproutResult).append('\r')
                            //生霉粒
                            stringBuilder.append("生霉粒(G)：").append(mRecordCorn.moldWeight).append('\r')
                            //生霉粒率
                            stringBuilder.append("生霉粒率(%)：").append(mRecordCorn.moldResult).append('\r')
                            //热损伤粒质量
                            stringBuilder.append("热损伤粒质量(G)：").append(mRecordCorn.thermalDamageGrainQuality).append('\r')
                            //热损伤粒结果
                            stringBuilder.append("热损伤粒率(%)：").append(mRecordCorn.thermalDamageGrain).append('\r')
                            //霉变粒质量
                            stringBuilder.append("霉变粒质量(G)：").append(mRecordCorn.mildewGrainQuality).append('\r')
                            //霉变粒结果
                            stringBuilder.append("霉变粒率(%)：").append(mRecordCorn.mildewGrain).append('\r')
                        }else{
                            //不完善粒质量
                            stringBuilder.append("不完善粒质量(G)：").append(mRecordCorn.imperfectGrainQuality).append('\r')
                        }
                        //不完善粒-结果
                        stringBuilder.append("不完善粒率(%)：").append(mRecordCorn.resultOfImperfectGrain).append('\r')
                        //不完善粒记录员
                        stringBuilder.append("不完善粒记录员：").append(mRecordCorn.imperfectGrainRecorder).append('\r')
                    }
                }
                "容重" -> {
                    //容重记录员
                    if (!TextUtils.isEmpty(mRecordCorn.densityRecorder)) {
                        stringBuilder.append("\r")
                        var i = 0
                        while (i < (1.0 - 4 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("容重").append('\r')
                        stringBuilder.append("\r")

                        //容重-结果
                        stringBuilder.append("容重-质量：").append(mRecordCorn.densityQuality).append('\r')
                        //容重-结果
                        stringBuilder.append("容重-结果：").append(mRecordCorn.densityResult).append('\r')
                        //容重记录员
                        stringBuilder.append("容重记录员：").append(mRecordCorn.densityRecorder).append('\r')
                    }
                }
            }
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
            InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a (100))
        }

    }
}